package com.hudongwx.my_shian.fragment.communityFragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.my_shian.R;
import com.hudongwx.my_shian.utils.API;
import com.hudongwx.my_shian.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
public class Health_3_Fragment extends Fragment {
    private RecyclerView recyclerView;
    private View view;
    private JSONArray data;
    private MyHealthAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (null == view) {
            view = inflater.inflate(R.layout.fragment_health_3, container, false);
            //获取控件
            recyclerView = (RecyclerView) view.findViewById(R.id.fragment_health_rv);
            //管理器
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            //适配器
            adapter = new MyHealthAdapter(data);
            //绑定适配器到listView
            recyclerView.setAdapter(adapter);

            updateData();
        }

        return view;
    }

    //请求数据
    public void updateData() {
        RequestParams params = new RequestParams(API.getCommunityHealthUrl());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    adapter.updateData(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    //继承一个recycler的viewHolder,实现自己viewHolder
    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView1;
        ImageView imageView2;
        ImageView imageView3;
        ImageView imageView4;

        TextView textView1;
        TextView textView2;
        TextView textView3;
        TextView textView4;
        TextView textView5;

        public MyViewHolder(View itemView) {
            super(itemView);

            //找出需要显示的控件
            textView1 = (TextView) itemView.findViewById(R.id.fragment_health_3_item_tv1);
            textView2 = (TextView) itemView.findViewById(R.id.fragment_health_3_item_tv2);
            textView3 = (TextView) itemView.findViewById(R.id.fragment_health_3_item_tv3);
            textView4 = (TextView) itemView.findViewById(R.id.fragment_health_3_item_tv4);
            textView5 = (TextView) itemView.findViewById(R.id.fragment_health_3_item_tv5);

            imageView1 = (ImageView) itemView.findViewById(R.id.fragment_health_3_item_iv1);
            imageView2 = (ImageView) itemView.findViewById(R.id.fragment_health_3_item_iv2);
            imageView3 = (ImageView) itemView.findViewById(R.id.fragment_health_3_item_iv3);
            imageView4 = (ImageView) itemView.findViewById(R.id.fragment_health_3_item_iv4);

        }
    }


    //适配器
    class MyHealthAdapter extends RecyclerView.Adapter<MyViewHolder> {

        JSONArray data;

        public MyHealthAdapter(JSONArray data) {
            this.data = data;
        }

        public void updateData(JSONArray data) {
            this.data = data;
            notifyDataSetChanged();
        }


        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            //解析一个布局
            View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_health_3_item, null);
            //通过view创建一个ViewHolder
            MyViewHolder viewHolder = new MyViewHolder(view1);

            return viewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            JSONArray arr = null;
            try {
                arr = data.getJSONObject(position).getJSONArray("ListFile");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            //把数据设置给控件
            try {
                if (arr.length() == 1) {
                    x.image().bind(holder.imageView2, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                } else if (arr.length() == 2) {
                    x.image().bind(holder.imageView2, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                    x.image().bind(holder.imageView3, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));

                } else if (arr.length() >= 3) {
                    x.image().bind(holder.imageView2, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                    x.image().bind(holder.imageView3, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));
                    x.image().bind(holder.imageView4, data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(2).getString("Url"));
                }

                x.image().bind(holder.imageView1, data.getJSONObject(position).getString("UserIcon"));

                holder.textView1.setText(data.getJSONObject(position).getString("UserName"));
                holder.textView2.setText(data.getJSONObject(position).getString("DateTime"));
                holder.textView3.setText(data.getJSONObject(position).getString("Title"));
                holder.textView4.setText(data.getJSONObject(position).getString("Content"));
                if ((data.getJSONObject(position).getString("CommentCount")=="0")){
                    holder.textView5.setText("回帖");
                }else {
                    holder.textView5.setText(data.getJSONObject(position).getString("CommentCount"));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            if (null == data) return 0;
            return data.length();
        }

    }

}
